//
//  notReceivedViewController.m
//  giuseppe
//
//  Created by Apple on 15/8/13.
//  Copyright (c) 2015年 GiantUmbrella. All rights reserved.
//

#import "notReceivedViewController.h"

#import "myorderModel.h"
#import "orderTableViewCell.h"

#import "CommentViewController.h"
#import "PaymentViewController.h"
#import "afterSalesServiceViewController.h"
#import "progressSearchViewController.h"

@interface notReceivedViewController ()<MBProgressHUDDelegate>{
    
    
    UIButton *payTableViewButton;
    UIButton *confirmReceivedTableViewButton;
    NSMutableArray *myorderModelArray;
    UITableView * _orderTableView;
    UIView *slideView;
    
    NSString *typeString;
    MBProgressHUD *HUD;
    
}

@end

@implementation notReceivedViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIView *navView1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    navView1.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:navView1];

    //自定义的导航栏
    UIView *navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 64)];
    navView.backgroundColor = [UIColor whiteColor];
    navView.alpha = 0.90;
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(10, 33, 27, 16);
    [backButton setBackgroundImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth/2-50, 20, 100, 40)];
    titleLabel.text = @"未收货";
    titleLabel.textAlignment = NSTextAlignmentCenter;

    //默认orderType
    typeString = [NSString stringWithFormat:@"3"];

    [self initButtonView];
    [self initTableView];

    [navView addSubview:backButton];
    [navView addSubview:titleLabel];
    [self.view addSubview:navView];
    

    // Do any additional setup after loading the view.
}
- (void)loadData{
        
    //请求参数
    

    NSDictionary *paras= @{@"userId":@"1",
                           @"type":typeString};
    //调用请求
    [[myDataAccess shardWebUtil] orderListsManagementWithURLString:kNetPath_Code_orderList andParas:paras andTransferGottenData:^(id obj, NSError *error)  {
        
        NSLog(@"%@",obj);
        
        if (obj) {
      
            
            NSArray *dataList = [obj objectForKey:@"dataList"];
            myorderModelArray = [[NSMutableArray alloc]init];
            
            for (NSDictionary *dic in dataList) {
                
                
                NSArray *detailList = [dic objectForKey:@"detailList"];
                
                for (NSDictionary *detail in detailList) {
                    
                    
                    myorderModel *model = [myorderModel initWithDicAttributes:detail];
                    
                    model.orderNumber = [dic objectForKey:@"orderNumber"];
                    model.addressId = [dic objectForKey:@"addressId"];
                    model.orderType = [dic objectForKey:@"orderType"];
                    
                    [myorderModelArray addObject:model];
                    
                }
                
                [_orderTableView.legendHeader endRefreshing];
                [_orderTableView reloadData];


        }
            
        }else{
            
            
            //没数据
            
        }
        
        
    }];

    
}

- (void)back{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

-(void)initButtonView{
    
    UIView *topLineView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, 1)];
    topLineView.backgroundColor = baseColor;

    [self.view addSubview:topLineView];
    
    payTableViewButton = [UIButton buttonWithType:UIButtonTypeCustom];
    payTableViewButton.frame = CGRectMake(0, 64, kScreenWidth/2, 40);
    [payTableViewButton setTitle:@"待付款" forState:UIControlStateNormal];
    [payTableViewButton setTitleColor:mainColor forState:UIControlStateNormal];
    [payTableViewButton addTarget:self action:@selector(payTableViewButtonAction) forControlEvents:UIControlEventTouchUpInside];

    confirmReceivedTableViewButton = [UIButton buttonWithType:UIButtonTypeCustom];
    confirmReceivedTableViewButton.frame = CGRectMake(kScreenWidth/2, 64, kScreenWidth/2, 40);
    [confirmReceivedTableViewButton setTitle:@"待收货" forState:UIControlStateNormal];
    [confirmReceivedTableViewButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [confirmReceivedTableViewButton addTarget:self action:@selector(confirmReceivedTableViewButtonAction) forControlEvents:UIControlEventTouchUpInside];
    
    

    UIView *groundLineView = [[UIView alloc]initWithFrame:CGRectMake(0, 64+40, kScreenWidth, 1)];
    groundLineView.backgroundColor = baseColor;
    [self.view addSubview:groundLineView];
    
    [self.view addSubview:payTableViewButton];
    [self.view addSubview:confirmReceivedTableViewButton];

    
    
    slideView = [[UIView alloc]initWithFrame:CGRectMake(0, 64+40-3, kScreenWidth/2, 3)];
    slideView.backgroundColor = mainColor;
    
    [self.view addSubview:slideView];
    
    
}



- (void)initTableView{
    
    
    _orderTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64+40, kScreenWidth, kScreenHeight-64-40)];
    _orderTableView.delegate = self;
    _orderTableView.dataSource = self;
    _orderTableView.rowHeight = 130;
    _orderTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_orderTableView];
    
    __weak __typeof(self) weakSelf = self;
    
    [_orderTableView  addLegendHeaderWithRefreshingBlock:^{
        
        [weakSelf loadData];
        
    }];
    
    [_orderTableView.legendHeader beginRefreshing];

    
}

#pragma mark--- tableViewDelegate 每个区的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return myorderModelArray.count;
    
}

#pragma mark--- tableViewDelegate 区尾的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    myorderModel *tmp = myorderModelArray[indexPath.row];
    
    if ([tmp.orderType isEqualToString:@"2"]) {
        return 160;
    }else{
        return _orderTableView.rowHeight;
    }
    
}


#pragma mark--- tableViewDelegate cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    static NSString *indentify = @"Cell";
    
    orderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentify];
    
    cell = [[orderTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentify];
    
    myorderModel *tmp = myorderModelArray[indexPath.row];
    
    cell.imageView.frame =CGRectMake(5, 10, 70, 110);
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:tmp.imgurl]];
    
    cell.nameLabel.frame = CGRectMake(80, 20, kScreenWidth - 80, 20);
    cell.nameLabel.text = tmp.name;
    
    cell.orderNumberLabel.frame = CGRectMake(80, 50, kScreenWidth - 80, 20);
    cell.orderNumberLabel.text = [NSString stringWithFormat:@"订单号:%@",tmp.orderNumber];
    
    cell.priceLabel.frame = CGRectMake(80, 80, 100, 30);
    cell.priceLabel.text = [NSString stringWithFormat:@"￥:%@",tmp.price];
    
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(15, 0, kScreenWidth-30, 1)];
    lineView.backgroundColor = baseColor;
    
    [cell.contentView addSubview:lineView];
    
    
    if (![tmp.amount isEqualToString:@"1"]) {
        cell.goodsNumberLabel.frame = CGRectMake(180, 85, 30, 20);
        cell.goodsNumberLabel.layer.masksToBounds = YES;
        cell.goodsNumberLabel.layer.cornerRadius = 3;
        cell.goodsNumberLabel.text = [NSString stringWithFormat:@"X%@",tmp.amount];
        
    }
    
    if (indexPath.row == 0) {
        
        lineView.frame = CGRectMake(0, 0, kScreenWidth, 1);
    }
    
    
    if ([tmp.orderType isEqualToString:@"1"]) {
        //评价
        cell.evaluationButton.frame = CGRectMake(kScreenWidth - 70, 90, 50, 20);
        cell.evaluationButton.tag = indexPath.row +200;
        
    }
    
    
    if ([tmp.orderType isEqualToString:@"2"]) {
        //确认收货
        cell.confirmReceivedButton.frame = CGRectMake(kScreenWidth - 70, 90, 50, 20);
        cell.confirmReceivedButton.tag = indexPath.row + 100;
        
        //灰色button
        //换货
        cell.exchangesButton.frame = CGRectMake(kScreenWidth-60, 130+5, 40, 20);
        cell.exchangesButton.tag = indexPath.row + 400;
        //退货
        cell.returnButton.frame = CGRectMake(cell.exchangesButton.frame.origin.x - 40 -5, 130+5, 40, 20);
        cell.returnButton.tag = indexPath.row + 500;
        //查看物流
        cell.LogisticsButton.frame = CGRectMake(cell.returnButton.frame.origin.x - 60 - 5, 130+5, 60, 20);
        cell.LogisticsButton.tag = indexPath.row + 600;
        
        
    }
    if ([tmp.orderType isEqualToString:@"3"]) {
        //付款
        cell.payButton.frame = CGRectMake(kScreenWidth - 70, 90, 50, 20);
        cell.payButton.tag = indexPath.row + 300;
        
    }
    if ([tmp.orderType isEqualToString:@"4"]) {
        cell.progressSearchButton.frame = CGRectMake(kScreenWidth - 70, 90, 50, 20);
        
        if ([tmp.afterSalesType isEqualToString:@"5"]) {
            cell.returnButton.frame = CGRectMake(210, 85, 50, 20);
        }
        if ([tmp.afterSalesType isEqualToString:@"6"]) {
            cell.exchangesButton.frame = CGRectMake(210, 85, 50, 20);
            
        }
        
        
    }
    
    //红色大按钮
    //评价按钮
    [cell.evaluationButton addTarget:self action:@selector(pushCommentVC:) forControlEvents:UIControlEventTouchUpInside];
    //确认收货按钮
    [cell.confirmReceivedButton addTarget:self action:@selector(confirmReceivedButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    //付款按钮
    [cell.payButton addTarget:self action:@selector(pushPayVC:) forControlEvents:UIControlEventTouchUpInside];
    
    //灰色小按钮
    //换货
    [cell.exchangesButton addTarget:self action:@selector(exchangesButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    //退货
    [cell.returnButton addTarget:self action:@selector(returnButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    //物流查询
    [cell.LogisticsButton addTarget:self action:@selector(LogisticsButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
    
    
}

#pragma mark--- tableViewDelegate 点击方法

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}



- (void)payTableViewButtonAction{
    
    
    
    [UIView animateWithDuration:0.3 animations:^(void){
        
        slideView.frame = CGRectMake(0, 64+40-3, kScreenWidth/2, 3);
        
        [payTableViewButton setTitleColor:mainColor forState:UIControlStateNormal];
        [confirmReceivedTableViewButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];

        typeString = @"3";
        [self initTableView];
        
    }completion:^(BOOL finish){
        

        
    }];

    
    
}


- (void)confirmReceivedTableViewButtonAction{
    
    
    
    [UIView animateWithDuration:0.3 animations:^(void){
        
        slideView.frame = CGRectMake(kScreenWidth/2, 64+40-3, kScreenWidth/2, 3);
        
        [confirmReceivedTableViewButton setTitleColor:mainColor forState:UIControlStateNormal];
        [payTableViewButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        
        
        typeString = @"2";
        [self initTableView];
    }completion:^(BOOL finish){
        

    }];

    
}



#pragma mark buttonAction
- (void)confirmReceivedButtonAction:(UIButton *)button{
    
    
    HUD = [MBProgressHUD showHUDAddedTo:_orderTableView animated:YES];
    HUD.delegate = self;
    HUD.mode = MBProgressHUDModeIndeterminate;
    HUD.labelText = @"确认中";
    
    NSString *suitId = [myorderModelArray[button.tag-100] suitid];
    NSString *orderId = [myorderModelArray[button.tag-100] orderNumber];
    
    
    //请求参数
    NSDictionary *paras= @{@"userId":@"1",
                           @"suitId":suitId,
                           @"orderId":orderId
                           };
    //调用请求0
    [[myDataAccess shardWebUtil] orderConfirmWithURLString:kNetPath_Code_orderConfirm  andParas:paras andTransferGottenData:^(id obj, NSError *error)  {
        
        NSLog(@"%@",obj);
        
        if (obj) {
            
            
            NSString *result = [obj objectForKey:@"resultCode"];
            
            if (result==0) {
                
                HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
                HUD.mode = MBProgressHUDModeCustomView;
                HUD.labelText = @"确认成功";
            }
            
            
        }else{
            
            //没数据
            
        }
        
        
    }];
    
    [self hudWasHidden:HUD];
    [self initTableView];
    
    
}

- (void)pushCommentVC:(UIButton *)button{
    
    CommentViewController *commentVC = [[CommentViewController alloc]init];
    
    
    
    commentVC.suitId = [myorderModelArray[button.tag-200] suitid];
    commentVC.orderId = [myorderModelArray[button.tag-200] orderNumber];
    
    [self.navigationController pushViewController:commentVC animated:YES];
    
    
    
}

- (void)pushPayVC:(UIButton *)button{
    
    PaymentViewController *paymentVC = [[PaymentViewController alloc]init];
    
    paymentVC.priceString = [NSString stringWithFormat:@"%d",[[myorderModelArray[button.tag-300] price] intValue]*[[myorderModelArray[button.tag-300] amount] intValue]];
    
    
    [self.navigationController pushViewController:paymentVC animated:YES];
    
    
}

- (void)LogisticsButtonAction:(UIButton *)button{
    
    progressSearchViewController *progressSearchVC = [[progressSearchViewController alloc]init];
    
    progressSearchVC.topicString = @"查看物流";
    
    progressSearchVC.imageUrlString = [myorderModelArray[button.tag-600] imgurl];
    progressSearchVC.nameString = [myorderModelArray[button.tag-600] name];
    progressSearchVC.orderNumberString = [myorderModelArray[button.tag-600] orderNumber];
    progressSearchVC.priceString = [myorderModelArray[button.tag-600] price];
    
    progressSearchVC.typeString = @"0";
    progressSearchVC.suitidString = [myorderModelArray[button.tag-600] suitid];
    [self.navigationController pushViewController:progressSearchVC animated:YES];
    
    
}

- (void)exchangesButtonAction:(UIButton *)button{
    
    afterSalesServiceViewController *afterSalesServiceVC = [[afterSalesServiceViewController alloc]init];
    
    afterSalesServiceVC.topicString = @"换货";
    afterSalesServiceVC.typeString = @"5";
    afterSalesServiceVC.imageUrlString = [myorderModelArray[button.tag-400] imgurl];
    afterSalesServiceVC.nameString = [myorderModelArray[button.tag-400] name];
    afterSalesServiceVC.orderNumberString = [myorderModelArray[button.tag-400] orderNumber];
    afterSalesServiceVC.priceString = [myorderModelArray[button.tag-400] price];
    //    afterSalesServiceVC.soldNumberString = [myorderModelArray[button.tag-400] soldNumberString];
    afterSalesServiceVC.sizeContentString = [myorderModelArray[button.tag-400] sizeContent];
    afterSalesServiceVC.amountString = [myorderModelArray[button.tag-400] amount];
    afterSalesServiceVC.suitidString = [myorderModelArray[button.tag-400] suitid];
    
    [self.navigationController pushViewController:afterSalesServiceVC animated:YES];
}


- (void)returnButtonAction:(UIButton *)button{
    
    afterSalesServiceViewController *afterSalesServiceVC = [[afterSalesServiceViewController alloc]init];
    
    afterSalesServiceVC.topicString = @"退货";
    afterSalesServiceVC.typeString = @"6";
    afterSalesServiceVC.imageUrlString = [myorderModelArray[button.tag-500] imgurl];
    afterSalesServiceVC.nameString = [myorderModelArray[button.tag-500] name];
    afterSalesServiceVC.orderNumberString = [myorderModelArray[button.tag-500] orderNumber];
    afterSalesServiceVC.priceString = [myorderModelArray[button.tag-500] price];
    //    afterSalesServiceVC.soldNumberString = [myorderModelArray[button.tag-400] soldNumberString];
    afterSalesServiceVC.sizeContentString = [myorderModelArray[button.tag-500] sizeContent];
    afterSalesServiceVC.amountString = [myorderModelArray[button.tag-500] amount];
    afterSalesServiceVC.suitidString = [myorderModelArray[button.tag-500] suitid];
    
    [self.navigationController pushViewController:afterSalesServiceVC animated:YES];
    
    
}

#pragma mark  MBProgressHUD delegate
-(void)hudWasHidden:(MBProgressHUD *)hud
{
    [HUD removeFromSuperview];
    HUD = nil;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
